package guinew.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.DefaultListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import redsig.signature.DocumentSigner;
import core.Adapter;

public class MainViewModel extends Observable{
	private String xmlFileName;
	private String privateKeyFileName;
	private DefaultListModel redactionQueueModel;
	private DefaultTreeModel treeModel;
	private DefaultMutableTreeNode treeNode;
	private DefaultMutableTreeNode selectedNode;
	private String publicKeyFileName;
	private boolean verified;
	
	public MainViewModel(){
		redactionQueueModel = new DefaultListModel();
		treeNode = new DefaultMutableTreeNode(); 
	}
	
	public void setModel(DefaultListModel model){
		this.redactionQueueModel = model;
		notifyObservers(this.redactionQueueModel);
	}
	
	public DefaultListModel getRedactionQueueModel(){
		return redactionQueueModel;
	}
	
	public DefaultMutableTreeNode getTreeNode(){
		return treeNode;
	}

	public boolean setXmlFile(File xmlFile) {		
		setXmlFileName(xmlFile.getAbsolutePath());
		redactionQueueModel.clear();
		//set the current file in the adapter 
		setTreeModel(new DefaultTreeModel(Adapter.setCurrentFile(xmlFile)));
		this.setChanged();
		notifyObservers(this);
		return Adapter.checkSignature();
	}

	public void setTreeModel(DefaultTreeModel treeModel) {
		this.treeModel = treeModel;
	}

	public DefaultTreeModel getTreeModel() {
		return treeModel;
	}

	public void setXmlFileName(String xmlFileName) {
		this.xmlFileName = xmlFileName;
	}

	public String getXmlFileName() {
		return xmlFileName;
	}

	public boolean isNodeInRedactionQueue(DefaultMutableTreeNode selectedNode) {
		return Adapter.isNodeInRedactionQueue(selectedNode);
	}

	public void setSelectedNode(DefaultMutableTreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public void addSelectedNodeToRedactionQueue() {
		Adapter.getRedactionQueue().add(selectedNode);
		redactionQueueModel.addElement(selectedNode);
		this.setChanged();
		notifyObservers(this);
	}

	public DefaultMutableTreeNode getSelectedNode() {
		return selectedNode;
	}

	public void removeFromRedactionQueue(DefaultMutableTreeNode selectedNode2) {
		Adapter.getRedactionQueue().remove(selectedNode2);
		redactionQueueModel.removeElement(selectedNode2);
		this.setChanged();
		notifyObservers(this);
	}

	public ArrayList<DefaultMutableTreeNode> getRedactionQueue() {
		return Adapter.getRedactionQueue();
	}

	public void redact() {
		Adapter.redact();
	}

	public File getFile() {
		return Adapter.getCurrentFile();
	}

	public void signDocument(File file) {
		setPrivateKeyFileName(file.getName());
		this.setChanged();
		notifyObservers();
		Adapter.processKey(file);
		Adapter.signDocument();
	}

	public void verifyDocument(File file) {
		setPublicKeyFileName(file.getName());
		Adapter.processCertificate(file);
		setVerified(Adapter.verifyDocument());
		this.setChanged();
		notifyObservers();
	}

	public void setVerified(boolean verified) {
		this.verified = verified;
	}

	public boolean isVerified() {
		return verified;
	}

	public void setPublicKeyFileName(String publicKeyFileName) {
		this.publicKeyFileName = publicKeyFileName;
	}

	public String getPublicKeyFileName() {
		return publicKeyFileName;
	}

	public void setPrivateKeyFileName(String privateKeyFileName) {
		this.privateKeyFileName = privateKeyFileName;
	}

	public String getPrivateKeyFileName() {
		return privateKeyFileName;
	}

}

